/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package weka;

import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import org.inra.qualscape.wekatexttoxml.WekaTextfileToXMLTextfile;

/**
 *
 * @author Utilisateur
 */
class exportXMLAction extends AbstractAction{
    
    private MainWindow fenetre;
    private String tree;

    public exportXMLAction(MainWindow fenetre, String tree,String texte) {
        super(texte);
        this.fenetre = fenetre;
        this.tree = tree;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        File file = null;
        JFileChooser choix = new JFileChooser();
        MonFiltre xml = new MonFiltre( 
           new String[]{"xml"},
           "Fichiers XML (*.xml)");
        choix.addChoosableFileFilter(xml);
        int retour = choix.showSaveDialog(fenetre);
        if (retour == JFileChooser.APPROVE_OPTION) {
        file = choix.getSelectedFile();
        fenetre.getEtude().setXmlPath(choix.getSelectedFile().getAbsolutePath());
        }
     File temp = new File("temp.txt");
     FileWriter fw = null;
     
     InputStream ips = null; 
        try {
            ips = new FileInputStream(temp);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(exportXMLAction.class.getName()).log(Level.SEVERE, null, ex);
        }
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
                        String chaine = "";
                        int compteur = 0;
                        
        try {
            while ((ligne=br.readLine())!=null){
                
                    if(ligne.contains("Number of Leaves"))
                        break;
                    if(compteur > 4)
                    {
                        chaine+=ligne+"\n";
                    }
                    
                    compteur++;
            }
        } catch (IOException ex) {
            Logger.getLogger(exportXMLAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            br.close();
        } catch (IOException ex) {
            Logger.getLogger(exportXMLAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            fw = new FileWriter (temp);
        } catch (IOException ex) {
            Logger.getLogger(exportXMLAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        
     BufferedWriter bw = new BufferedWriter (fw);
     PrintWriter fichierSortie = new PrintWriter (bw); 
     fichierSortie.println (chaine); 
     fichierSortie.close();                   
     WekaTextfileToXMLTextfile testxml = new WekaTextfileToXMLTextfile(temp, file, true, true);
     testxml.writeXmlFromWekaText();
     temp.delete();
     //fenetre.
    }
    
}
